***************
* This script produces tables and figures based on the estimates of the part-time penalty by gender and canton
* Author: Daniel Kopp
***************

	clear
	clear matrix
	clear mata
	set more off	
	set maxvar 8000

	import delimited using "Misc_files\cantons.csv", clear
	drop kanton
	
	cap drop y x
	cap set obs  10
	cap gen y = 1
	cap gen x = 1	
	reg y x

	nlcom (base0: _b[x]) (base1: _b[x]), post
	estimates store base0
		
	******************************************************************
	******************************************************************
	* Part-time penalty (for men and women separately) by canton:
	******************************************************************
	******************************************************************
	
	* Define a Label
	forv r = 1/`c(N)' {
		loc val = kanton_s[`r']
		loc lab = kanton_lab[`r']
		if (!mi(`val')==1) la def kanton_s `val' "`lab'", add
	}

	* temp save labels
	label save kanton_s using "Help_files/kant_lab.do", replace

	* apply label
	run "Help_files/kant_lab.do"
	label values kanton_s kanton_s
	numlabel kanton_s, add

	estimates use "$save_path\workvolume_by_kant_int"
	estimates store part_kant_int	
	
	estimates restore part_kant_int
		local mean_sample = e(mean_sample)
		local mean_disp = e(mean_disp)		
		local N = e(N)
		matrix b = e(b) 
		matrix b = b[1,1..78]
		matrix V = e(V)
		matrix V = V[1..78,1..78]
		local obs : disp %9.0f `N'
		quietly ereturn post b V, dep(contact_button_clicked) obs(`obs') 	
			estadd local mean_sample   " `mean_sample' "
			estadd local mean_disp   " `mean_disp' "			
			estadd local obs  " `obs' "															
	eststo part_kant_int_sm	
	
	esttab part_kant_int_sm, varwidth(45)
	
	* Note: For canton 10 (GR), the part-time coefficient is positive and the interaction negative
	* Note: For canton 24 (VS), the absolute value of the interaction is larger than the part-time coefficient
		
	forvalues i = 1/26 {
	* Difference between female and male part-time penalty per canton	
	estimates restore part_kant_int_sm
	local mean_disp = e(mean_disp)		
	nlcom		(parttime: _b[kanton_s_`i'XparttimeXgeschlecht]) , post
	estadd local mean_disp   " `mean_disp' "				
	estimates 	store diff_kant_`i'	
	}
		
	foreach x in  diff  {
	cap gen penalty_`x' = . 
	cap gen t_penalty_`x' =. 
	cap gen se_penalty_`x' =.
	cap gen lo_penalty_`x' = .
	cap gen hi_penalty_`x' = .				
	}

	forvalues i = 1/26 {
		foreach x in  diff  {

		estimates restore `x'_kant_`i'	
			cap	local 	penalty_`x' 		= _b[parttime] 
			cap	replace penalty_`x' 	 	= `penalty_`x'' 		if kanton_s==`i'		
			cap local 	t_penalty_`x' 	 	= abs(_b[parttime] / _se[parttime] )
			cap replace t_penalty_`x' 	 	= `t_penalty_`x'' 		if kanton_s==`i'		
			cap local 	se_penalty_`x' 	 	=  _se[parttime] 
			cap replace se_penalty_`x' 	 	= `se_penalty_`x'' 		if kanton_s==`i'		
			cap local 	lo_penalty_`x' 		=  _b[parttime] - 1.96 * _se[parttime] 
			cap replace lo_penalty_`x' 		= `lo_penalty_`x'' 		if kanton_s==`i'		
			cap local 	hi_penalty_`x' 		=  _b[parttime] + 1.96 * _se[parttime] 
			cap replace hi_penalty_`x' 		= `hi_penalty_`x'' 		if kanton_s==`i'			
		estimates drop  `x'_kant_`i'
		}
	}
	
	* Gen number of observations kanton
	gen no_obs_kant = . 
	forvalues i = 1/26  {		
		estimates use "$save_path\summary_kanton_s`i'"
		matrix list e(mean)
		matrix c = e(mean)
		local obs_`i' = e(N)
		global mean_`i' =  c[1,1]
		disp "`obs_`i''"
		disp "${mean_`i'}"
		replace no_obs_kant = `obs_`i'' if kanton_s==`i' 
	}
	
	
	******************************************
	******************************************
	* Relationship between vote share in favor of parental leave and part-time penalty
	******************************************
	******************************************
	
	merge 1:1 kanton_s using "Misc_files\abstimmung_vaterschaft.dta"
	drop _merge
		
	gen yes_share = 1-no_share
	
	label var no_share "Vote share against extending paternity leave"
	label var yes_share "Vote share in favor of extending paternity leave"
		
	foreach coef in penalty_diff {
		foreach x in yes_share {
		preserve 

		drop if t_`coef'==.	
		
		local msize "large"	
		local mlcolor "mlcolor(black)"	
		
		* Regression line:		
		local cond ""
		regress `coef' 	`x'  `cond'
			
		cap drop xb_`coef' sexb_`coef' hix_`coef' lox_`coef'		
		
		cap predict double xb_`coef'	, xb 
		
		cap predict double sexb_`coef' , stdp
		
		gen hix_`coef' = xb_`coef'+sexb_`coef'*1.96
		gen lox_`coef' = xb_`coef'-sexb_`coef'*1.96
					
		twoway  	(scatter `coef' 		`x'  				`cond',  mcolor(black) `mlcolor' mlwidth(thin) msize(`msize')) ///
					(line 	hix_`coef' 		`x'  				`cond',  msymbol(none) lwidth(medthin) lcolor(gs9) lpattern(dash)  sort ) ///
					(line 	lox_`coef'		`x'  				`cond',  msymbol(none) lwidth(medthin) lcolor(gs9) lpattern(dash)  sort ) ///
					(line 	xb_`coef'	 	`x'  				`cond',  msymbol(none) lwidth(medthin) lcolor(gs9) lpattern(solid))  ,    ///			
					graphregion(color(white))  legend(off) ///
					xtitle("Share of yes votes in parental leave referendum", size(medlarge)) ///
					ytitle("Gender gap in part-time penalty", size(medlarge)) 		
		graph export "$results_part_time/figure_d11_a.eps", as(eps) replace	
		* Save data in table
		drop x y _est_base0
		export delimited using "$results_part_time/figure_d11_a_table.csv", replace
		}
		restore
	}
